﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;

/// <summary>
/// Summary description for MyClass
/// </summary>
public static class MyClass
{

    public static string GetChangesAsXml(this DataTable dt, string TableName, params bool[] BringAll)
    {
        StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
        XmlTextWriter tw = new XmlTextWriter(sw);

        
        if (dt.DataSet == null)
        {
            DataSet ds = new DataSet("MyData");
            ds.Tables.Add(dt);
        }
        else
            dt.DataSet.DataSetName = "MyData";

        DataSet dsChanges;
        if (BringAll.Length > 0)
            dsChanges = dt.DataSet;
        else
            dsChanges = dt.DataSet.GetChanges();
        if (dsChanges == null)
            return "";

        DataTable dtChanges = dsChanges.Tables[dt.TableName];

        for (int i = dtChanges.Columns.Count - 1; i >= 0; i--)
        {
            if (dtChanges.Columns[i].DataType == typeof(System.Byte[]))
                dtChanges.Columns.RemoveAt(i);
            else if (dtChanges.Columns[i].DataType == typeof(DateTime))
                dtChanges.Columns[i].DateTimeMode = DataSetDateTime.Unspecified;
        }

        dtChanges.TableName = TableName;
        dtChanges.DataSet.WriteXml(tw, XmlWriteMode.IgnoreSchema);

        return sw.ToString();
    }
    public static decimal ToDecimalNN(this object value)
    {
        decimal result = -1;

        if (value == DBNull.Value)
        {
        }
        else if (value == null)
        {
        }
        else
        {
            try
            {
                result = Convert.ToDecimal(value, CultureInfo.CurrentCulture);

                //CultureInfo ci = new CultureInfo("en-US");
                //result = Convert.ToDecimal(value, ci);
            }
            catch
            {

            }
        }
        return result;
    }

    public static decimal ToDecimalZero(this object value)
    {
        decimal result = 0;

        if (value == DBNull.Value)
        {
        }
        else if (value == null)
        {
        }
        else
        {
            try
            {
                result = Convert.ToDecimal(value, CultureInfo.CurrentCulture);

                //CultureInfo ci = new CultureInfo("en-US");
                //result = Convert.ToDecimal(value, ci);
            }
            catch
            {

            }
        }
        return result;
    }

    /// <summary>
    /// Bu fonksyion, aldığı object türünde veriyi integer a çevirir. Çeviremediği durumlarda -1 dönderir.
    /// </summary>
    /// <param name="value">Oject türünde herhangi bir parametre. Null ya da DBNull.Value da olabilir.</param>
    /// <returns>Integer a dönüştürülmüş hali ya da hata durumunda -1</returns>
    public static int ToIntNN(this object value)
    {
        int result = -1;

        if (value == DBNull.Value)
        {
        }
        else if (value == null || value.ToString() == "")
        {
        }
        else
        {
            try
            {
                result = Convert.ToInt32(value);
            }
            catch
            {
            }
        }
        return result;
    }

    public static object ToIntDBNull(this object value)
    {
        if (value == DBNull.Value)
        {
        }
        else if (value == null || value.ToString() == "")
        {
        }
        else
        {
            try
            {
                int result = Convert.ToInt32(value);
                return result;
            }
            catch
            {
            }
        }
        return DBNull.Value;
    }

    public static int ToIntZero(this object value)
    {
        int result = 0;

        if (value == DBNull.Value)
        {
        }
        else if (value == null || value.ToString() == "")
        {
        }
        else
        {
            try
            {
                result = Convert.ToInt32(value);
            }
            catch
            {
            }
        }
        return result;
    }
}